package one.stand.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import one.stand.aop.ApiController;
import one.stand.model.ResultModel;
import one.stand.request.*;
import one.stand.response.OrdersResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.List;
import java.util.Map;

/**
 * 订单信息
 * @author Administrator
 */
@RestController
@RequestMapping("userApi/orders")
@Api(description = "订单系列")
public class OrdersController extends BaseController {

    @PostMapping("/selectShopcart")
    @ApiOperation(value = "查询我的购物车")
    @ApiImplicitParams({})
    @ApiController(isLogin = true,paramClass =ShopcartRequest.class )
    public ResultModel selectShopcartgetUserInfo(@RequestAttribute ShopcartRequest request) {
        return userFegin.selectShopcart(request);
    }

    @PostMapping("/addShopcart")
    @ApiOperation(value = "保存商品到购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "specId", value = "规格ID", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "num", value = "数量", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "forwardId", value = "转发id", paramType = "Integer"),
            @ApiImplicitParam(name = "cardId", value = "名片ID（用户进入企业商城时选择的名片ID）", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,paramClass =ShopcartRequest.class)
    public ResultModel addShopcart(@RequestAttribute ShopcartRequest request) {

        return userFegin.addShopcart(request);
    }

    @PostMapping("/delShopcart")
    @ApiOperation(value = "删除购物车商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopCartIds", value = "购物车id(逗号隔开)", required = true, paramType = "String"),
    })
    @ApiController(isLogin = true,paramClass =ShopcartRequest.class)
    public ResultModel delShopcart(@RequestAttribute ShopcartRequest request) {

        return userFegin.delShopcart(request);
    }

    @PostMapping("/updShopcartNum")
    @ApiOperation(value = "修改购物车商品数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopCartId", value = "购物车id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "num", value = "数量", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,paramClass =ShopcartRequest.class)
    public ResultModel updShopcartNum(@RequestAttribute ShopcartRequest request) {

        return userFegin.updShopcartNum(request);
    }

    @PostMapping("/directOrder")
    @ApiOperation(value = "商品直接下单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "specId", value = "规格ID", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "num", value = "数量", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "remark", value = "备注", paramType = "String"),
            @ApiImplicitParam(name = "allPrice", value = "订单金额", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "forwardId", value = "转发id", paramType = "Integer"),
            @ApiImplicitParam(name = "cardId", value = "名片ID（用户进入企业商城时选择的名片ID）", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,paramClass =OrderinfoRequest.class)
    public ResultModel directOrder(@RequestAttribute OrderinfoRequest request) {

        return userFegin.directOrder(request);
    }

    @PostMapping("/getOrderList")
    @ApiOperation(value = "查询用户订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderState", value = "订单状态（1待付款，2待发货，3待收货，4已完成，5交易取消，6退款成功）", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,paramClass =OrdersRequest.class)
    public ResultModel getOrderList(@RequestAttribute OrdersRequest request) {
        return userFegin.getOrderList(request);
    }

    @PostMapping("/getOrderListCount")
    @ApiOperation(value = "查询用户订单数量")
    @ApiController(isLogin = true,isParam = false)
    public ResultModel<List<Map<String, Object>>> getOrderListCount(OrdersRequest request,@RequestAttribute TokenRequest tokenRequest) {
        BeanUtils.copyProperties(tokenRequest,request);
        return userFegin.getOrderListCount(request);
    }

    @PostMapping("/goPay")
    @ApiOperation(value = "去支付（旧）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单ID", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "addressId", value = "地址id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "remark", value = "备注", paramType = "String"),
    })
    @ApiController(isLogin = true,paramClass = OrdersRequest.class)
    public ResultModel<Map<String, String>> goPay(@RequestAttribute OrdersRequest request) {
        return userFegin.goPay(request);
    }

    @PostMapping("/goPayNew")
    @ApiOperation(value = "去支付（新-3.2版本迭代 + 拼团版本assembleId）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "specId", value = "规格ID", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "num", value = "数量", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "forwardId", value = "转发id", paramType = "Integer"),
            @ApiImplicitParam(name = "level", value = "转发等级", paramType = "Integer"),
            @ApiImplicitParam(name = "cardId", value = "名片ID（用户进入企业商城时选择的名片ID）", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "addressId", value = "地址id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "assembleId", value = "拼团id", paramType = "Integer"),
            @ApiImplicitParam(name = "remark", value = "备注", paramType = "String"),
            @ApiImplicitParam(name = "orderinfoRequestList", value = "商品信息", required = true, paramType = "List"),
    })
    @ApiController(isLogin = true,paramClass = OrdersRequest.class)
    public ResultModel<Map<String, String>> goPayNew(@RequestAttribute OrdersRequest request) {
        return userFegin.goPayNew(request);
    }

    @PostMapping("/goTwoPay")
    @ApiOperation(value = "去支付（下单未支付后再次支付）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单ID", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,paramClass = OrdersRequest.class)
    public ResultModel<Map<String, String>> goTwoPay(@RequestAttribute OrdersRequest request) {
        return userFegin.goTwoPay(request);
    }

    @RequestMapping(value = "/paymentOrders")
    @ApiController(isParam = false)
    public String paymentOrders(HttpServletRequest request) {
        try {
            BufferedReader reader = request.getReader();
            String line = "";
            StringBuffer inputString = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                inputString.append(line);
            }
            request.getReader().close();
            return userFegin.paymentOrders(inputString.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return fail();
    }

    public String fail() {
        return "<xml> \n" +
                "  <return_code><![CDATA[fail]]></return_code>\n" +
                "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                "</xml>";
    }

    @PostMapping("/refundOrder")
    @ApiOperation(value = "用户提交退款申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单ID", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "refundRemark", value = "退款原因", required = true, paramType = "String"),
            @ApiImplicitParam(name = "formId", value = "表单提交场景下，为 submit 事件带上的 formId；支付场景下，为本次支付的 prepay_id", required = true, paramType = "String"),
            @ApiImplicitParam(name = "page", value = "点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。", paramType = "String"),
    })
    @ApiController(isLogin = true,paramClass = OrdersRequest.class)
    public ResultModel refundOrder(@RequestAttribute OrdersRequest request) {
        return userFegin.refundOrder(request);
    }

    @PostMapping("/backRefundState")
    @ApiOperation(value = "用户撤销退款申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单ID", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,paramClass = OrdersRequest.class)
    public ResultModel backRefundState(@RequestAttribute OrdersRequest request) {
        return userFegin.backRefundState(request);
    }

    @PostMapping("/shopcartOrder")
    @ApiOperation(value = "购物车下单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopCartIds", value = "购物车ID", required = true, paramType = "String"),
            @ApiImplicitParam(name = "money", value = "购物车总金额金额", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,paramClass = ShopcartRequest.class)
    public ResultModel<String> shopcartOrder(@RequestAttribute ShopcartRequest request) {
        return userFegin.shopcartOrder(request);
    }

    @PostMapping("/shopAddCollection")
    @ApiOperation(value = "购物车添加收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemIds", value = "商品id（逗号隔开）", required = true, paramType = "String"),
            @ApiImplicitParam(name = "itemType", value = "收藏对象类型（1产品，2文章，3企业）", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,paramClass = CollectionRequest.class)
    public ResultModel shopAddCollection(@RequestAttribute CollectionRequest request) {
        return userFegin.shopAddCollection(request);
    }

    @PostMapping("/getOrderInfo")
    @ApiOperation(value = "下单时订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersIds", value = "订单id(逗号隔开)", required = true, paramType = "String"),
    })
    @ApiController(isLogin = true,paramClass = OrdersRequest.class)
    public ResultModel<List<OrdersResponse>> getOrderInfo(@RequestAttribute OrdersRequest request) {
        return userFegin.getOrderInfo(request);
    }

    @PostMapping("/getPayOrderInfo")
    @ApiOperation(value = "待支付时订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersIds", value = "订单id", required = true, paramType = "String"),
    })
    @ApiController(isLogin = true,paramClass = OrdersRequest.class)
    public ResultModel<List<OrdersResponse>> getPayOrderInfo(@RequestAttribute OrdersRequest request) {
        return userFegin.getPayOrderInfo(request);
    }

    @PostMapping("/updateOrderState")
    @ApiOperation(value = "修改订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单id", required = true, paramType = "String"),
            @ApiImplicitParam(name = "orderState", value = "订单状态（1待付款，2待发货，3待收货，4已完成，5交易取消，6退款成功）", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,paramClass = OrdersRequest.class)
    public ResultModel updateOrderState(@RequestAttribute OrdersRequest request) {
        return userFegin.updateOrderState(request);
    }

    @PostMapping("/doReminder")
    @ApiOperation(value = "提醒发货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "formId", value = "表单提交场景下，为 submit 事件带上的 formId；支付场景下，为本次支付的 prepay_id", required = true, paramType = "String"),
            @ApiImplicitParam(name = "page", value = "点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。", paramType = "String"),
    })
    @ApiController(isLogin = true,paramClass = OrdersRequest.class)
    public ResultModel doReminder(@RequestAttribute OrdersRequest request) {
        return userFegin.doReminder(request);
    }

    @PostMapping("/anotherOrder")
    @ApiOperation(value = "再来一单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单id", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,paramClass = OrdersRequest.class)
    public ResultModel anotherOrder(@RequestAttribute OrdersRequest request) {
        return userFegin.anotherOrder(request);
    }

    @PostMapping("/openOrJoinAssemble")
    @ApiOperation(value = "开团/参团验证接口（返回拼团id）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "assembleId", value = "拼团id（参团传值，开团不传）", paramType = "Integer"),
    })
    @ApiController(isLogin = true, paramClass = AssembleRequest.class)
    public ResultModel<Integer> openOrJoinAssemble(@RequestAttribute AssembleRequest request) {
        return userFegin.openOrJoinAssemble(request);
    }
}
